package com.newbingo.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.googlecode.jsonplugin.annotations.JSON;
import com.newbingo.entity.Backup;
import com.newbingo.service.imple.IBackupService;
import com.newbingo.util.*;

public class BackAction extends ActionSupport {

	private IBackupService backupService = null;
	
	private Backup backup=new Backup();

	private Pager pager = null;
	private int start;
	private int limit = 15;

	private boolean success = false;
	
	private String fileName;

//*****************操作方法*******************

	/*
	 * 备份
	 */
	public String backup(){
		Date date=new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss");
		String strDate = sdf.format(date);
		String strName = "newbingo(" + strDate + ")";
		if (backupService.backup(strName)) {
			backup.setFileName(strName);
			backup.setBackupTime(date);
			backupService.save(backup);
			success=true;
		}else {
			success=false;
		}
//		return SUCCESS;
		return "back";
	}
	
	/*
	 * 还原
	 */
	public String restore(){
		if (backupService.restore(fileName)) {
			success=true;
		}else {
			success=false;
		}
//		return SUCCESS;
		return "restore";
	}
	
	/*
	 * 删除
	 */
	public String delete(){
		if (backupService.deleteFile(backup.getFileName())) {
			backupService.delete(backup);
			success=true;
		}else {
			success=false;
		}
//		return SUCCESS;
		return "delete";
	}
	
	/*
	 * 查询
	 */
	public String findByPage(){
		this.setPager(backupService.findByPage(start, limit));
		success=true;
		return SUCCESS;
	}
	
	
	
	@JSON(serialize = false)
	public IBackupService getBackupService() {
		return backupService;
	}

	public void setBackupService(IBackupService backupService) {
		this.backupService = backupService;
	}

	@JSON(serialize = false)
	public Pager getPager() {
		return pager;
	}

	public void setPager(Pager pager) {
		this.pager = pager;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public Backup getBackup() {
		return backup;
	}

	public void setBackup(Backup backup) {
		this.backup = backup;
	}
	
	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}


}
